package com.topnav.mc.admin.entity.wrapper;

import com.topnav.mc.admin.entity.param.DeviceParam;
import com.topnav.mc.admin.entity.param.OrganizationParam;
import com.topnav.mc.base.mc.McWrapper;
import org.springframework.util.StringUtils;

public class OrganizationWrapper extends McWrapper<OrganizationParam> {
    public OrganizationWrapper(OrganizationParam entity) {
        super(entity);
        if (this.permissionGrade != null ){
            if (this.permissionGrade.getPermissionLevel() == 1){
                this.eq("o.id",this.permissionGrade.getOrgId());
            }else if (this.permissionGrade.getPermissionLevel() == 2){
                this.eq("d.id",this.permissionGrade.getOrgId());
            }else if (this.permissionGrade.getPermissionLevel() == 3 || this.permissionGrade.getPermissionLevel() == 4){
                this.likeRight("o.path",this.permissionGrade.getOrgPath());
            }
        }

        if (entity != null){
            if (entity.getId()!=null){this.eq("o.id",entity.getId());}
            if (entity.getParentId()!=null){this.eq("o.parent_id",entity.getParentId());}
            if (!StringUtils.isEmpty(entity.getCode())){this.like("o.code",entity.getCode());}
            if (!StringUtils.isEmpty(entity.getName())){this.like("o.name",entity.getName());}
            if (!StringUtils.isEmpty(entity.getShortName())){this.like("o.short_name",entity.getShortName());}
            if (!StringUtils.isEmpty(entity.getPath())){this.likeRight("o.path",entity.getPath());}

            if (!StringUtils.isEmpty(entity.getKeyword())){
                this.and(wrapper -> wrapper.like("o.code", entity.getKeyword())
                        .or().like("o.name", entity.getKeyword())
                );
            }

            if (entity.getToplayer() != null){
                if (entity.getToplayer()){
                    if (this.permissionGrade.getOrgId() == null) {
                        this.isNull("o.parent_id");
                    }else{
                        this.eq("o.id",this.permissionGrade.getOrgId());
                    }
                }
            }
            this.orderByDesc("o.id");
        }
    }
}
